package com.cc.dlnalib

import android.content.Context
import android.net.ConnectivityManager

/**
 * 网络是否可用
 */
fun Context.isNetworkAvailable(): Boolean {
    val mConnectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val mNetworkInfo = mConnectivityManager.activeNetworkInfo
    if (mNetworkInfo != null) {
        return mNetworkInfo.isAvailable
    }
    return false
}

fun Context.isWifiConnected(): Boolean {
    val mConnectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val mWiFiNetworkInfo = mConnectivityManager
        .getNetworkInfo(ConnectivityManager.TYPE_WIFI)
    if (mWiFiNetworkInfo != null) {
        return mWiFiNetworkInfo.isAvailable
    }
    return false
}

fun getFormatTime(seconds: Int): String {
    val h = String.format("%02d", seconds / 3600)
    val m = String.format("%02d", seconds % 3600 / 60)
    val s = String.format("%02d", seconds % 60)
    return "$h:$m:$s"
}

fun timeToInt(time: String): Int {
    val array = time.split(":")
    if (array.size == 3) {
        val h = array[0].toInt()
        val m = array[1].toInt()
        val s = array[2].toInt()
        return h * 3600 + m * 60 + s
    } else {
        return 0
    }
}